package com.qf.day13.homework;

import jdk.management.resource.internal.inst.SocketOutputStreamRMHooks;

import java.util.*;


public class Homework2 {
    public static void main(String[] args) {
        HashMap<String,String>hashMap=new HashMap<>();
        hashMap.put("2006","意大利");
        hashMap.put("2002","巴西");
        hashMap.put("1998","法国");
        hashMap.put("1994","巴西");
        hashMap.put("1990","德国");
        hashMap.put("1986","阿根廷");

        hashMap.put("1982","意大利");
        hashMap.put("1978","阿根廷");
        hashMap.put("1974","德国");
        hashMap.put("1970","巴西");
        hashMap.put("1966","英格兰");

        hashMap.put("1962","巴西");
        hashMap.put("1958","巴西");
        hashMap.put("1954","德国");
        hashMap.put("1950","乌拉圭");

        hashMap.put("1938","意大利");
        hashMap.put("1934","意大利");
        hashMap.put("1930","乌拉圭");
        System.out.println(hashMap);
        Scanner scanner=new Scanner(System.in);
        while(true){

            System.out.println("1.查询该年的世界杯冠军是哪支球队\t2.读入一支球队的名字，输出该球队夺冠的年份列表\t3.退出");
            int a=scanner.nextInt();
            switch (a){
                case 1:
                    method1(hashMap);
                    break;
                case 2:
                    method2(hashMap);
                    break;
                case 3:
                    System.out.println(886);
                    return;
                default:
                    System.out.println("输入错误,请重新输入");
                    break;
            }
        }
    }

    private static void method2(HashMap<String, String> hashMap) {
        System.out.println("请输入一只球队的名字");
        Scanner scanner=new Scanner(System.in);
        String team=scanner.next();
        if(hashMap.containsValue(team)){
            ArrayList<String> keys = new ArrayList<>();
            //增强for  遍历hashMap集合,如队名和值一样,就把stringStringEntry的键填入keys,里面有getValue和getKey方法
            Set<Map.Entry<String, String>> entries = hashMap.entrySet();
            //hashMap.for
            //            for (Map.Entry<String, String> entry : entries) {
            //
            //            }
            for (Map.Entry<String, String> stringStringEntry : entries) {
                if(team.equals(stringStringEntry.getValue())){
                    keys.add(stringStringEntry.getKey());
                }

            }
            System.out.println(keys);

        }else{
            System.out.println(team+"没有获得过世界杯冠军");
        }

    }

    private static void method1(HashMap<String,String> hashMap){
        System.out.println("请输入一个年份");
        Scanner scanner=new Scanner(System.in);
        String year=scanner.next();
        if(Integer.valueOf(year)<1930||Integer.valueOf(year)>2006){
            System.out.println("请输入正确的年份");
            return;
        }else{
            if(hashMap.containsKey(year)){
                System.out.println(year+"年的世界杯冠军是====="+hashMap.get(year)+"=====球队");
            }else{
                System.out.println(year+"没有举办世界杯。");
            }

        }
    }
}
